<?php 

class Wallet extends Eloquent{
	protected $table = 'wallet';
	protected $primaryKey='user_id';
	
	public static function  balance($money){
		$wallet = self::getWallet();
		return(bool)($wallet->money > $money);
	}

	
	public static function getWallet($user_id = null){
		if(!$user_id) $user_id = Auth::user()->id;
		$wallet = Wallet::find($user_id);
		if(!$wallet){
			$wallet = new Wallet();
			$wallet->user_id = $user_id;
			$wallet->money = 0;
			$wallet->save();
		}
		return $wallet;
	}
	
	public static function getMoney($user_id = null) {
	    $wallet = self::getWallet($user_id);
	    return $wallet->money;
	}

	public function recharge($user_id, $money, $message, $order_no, $channel) {
		$this->money = $this->money + $money;
		$this->save();
		$myWalletTransaction = WalletTransaction::createRecharge(
		    $user_id, $order_no, $money, $message, WalletTransaction::STATUS_SUCCESS, $channel);
	}
	
	public function reward($money, $message, $order_no, $to_user_id, $from_user_id, $channel) {
		$this->money = $this->money + $money;
		$this->save();
		$myWalletTransaction = WalletTransaction::createReward($from_user_id, $to_user_id,
			$money, $message, $order_no, $channel);
	}
	
	
}